﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyNhaSach.BusinessLogicLayer
{
    class QuanLySachBLL
    {

        /// <summary>
        /// do thằng mập
        /// </summary>
        public QuanLySachBLL()
        {

        }

        private int DemChuSo(int number)
        {
            return number < 10 ? 1 : 1 + DemChuSo(number / 10);                            
        }

        public DataTable XemThongTinSach(string masach)
        {
            DataAccessLayer.SachDAO s = new DataAccessLayer.SachDAO();
            return s.XemThongTinSach(masach);
        }

        public DataTable LayDanhSach()
        {
            DataAccessLayer.SachDAO d = new DataAccessLayer.SachDAO();           
            return d.LayDanhSach();            
        }


        public void CapNhatGiaSach(string ma, string gia)
        {
            DataAccessLayer.GiaDAO _gia = new DataAccessLayer.GiaDAO();
            _gia.CapNhatGia(ma, gia);
        }

        public void CapNhatSL(string ma, string sl)
        {
            DataAccessLayer.SachDAO sach = new DataAccessLayer.SachDAO();
            sach.CapNhatSach(ma, sl);
        }

        public DataTable TimKiemSach(string key)
        {
            DataAccessLayer.SachDAO sach = new DataAccessLayer.SachDAO();
            return sach.TimKiem(key);
        }
        /////////////

        public DataTable TimKiemTheoMa(string key)
        {
            DataAccessLayer.SachDAO sach = new DataAccessLayer.SachDAO();
            return sach.TimKiemTheoMa(key);
        }

        public DataTable TimKiemTheoTen(string key)
        {
            DataAccessLayer.SachDAO sach = new DataAccessLayer.SachDAO();
            return sach.TimKiemTheoTen(key);
        }

        public DataTable TimKiemTheoTacGia(string key)
        {
            DataAccessLayer.SachDAO sach = new DataAccessLayer.SachDAO();
            return sach.TimKiemTacGia(key);
        }

        public DataTable TimKiemTheLoai(string key)
        {
            DataAccessLayer.SachDAO sach = new DataAccessLayer.SachDAO();
            return sach.TimKiemTheoTheLoai(key);
        }

        public int LayGiaBan(string ma)
        {
            DataAccessLayer.GiaDAO _gia = new DataAccessLayer.GiaDAO();
            DataTable data = _gia.LayGiaSach(ma);            
            return (int)float.Parse(data.Rows[0]["GiaBan"].ToString());
        }

        public string TaoMaSach()
        {
            string result = "S";
            DataAccessLayer.SachDAO d = new DataAccessLayer.SachDAO();
            DataTable data = d.LayDanhSach();
            int count = data.Rows.Count + 1;
            if (count >= 9999) throw new Exception("Het khong gian ma sach");
            int c = DemChuSo(count);
            for (int i = 0; i < (4 - c); i++)
            {
                result += "0";
            }
            result += count.ToString();
            return result;
        }

        public void ThemSach(string ma,string ten, string theloai, string tacgia)
        {
            DataAccessLayer.SachDAO d = new DataAccessLayer.SachDAO();
            d.ThemSach(ma, ten, theloai, tacgia);
        }

        public void CapNhatSach(string ma, string ten, string theloai, string tacgia,string soluong)
        {
            DataAccessLayer.SachDAO d = new DataAccessLayer.SachDAO();
            d.CapNhatSach(ma, ten, theloai, tacgia, soluong);
        }


        public AutoCompleteStringCollection TaoAutocompleteTen()
        {
            DataAccessLayer.SachDAO d = new DataAccessLayer.SachDAO();
            DataTable data = d.LayDanhSach();
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            foreach (DataRow x in data.Rows)
            {
                auto.Add(x["TenSach"].ToString());
            }
            return auto;
        }

        public AutoCompleteStringCollection TaoAutocompleteMa()
        {
            DataAccessLayer.SachDAO d = new DataAccessLayer.SachDAO();
            DataTable data = d.LayDanhSach();
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            foreach (DataRow x in data.Rows)
            {
                auto.Add(x["MaSach"].ToString());
            }
            return auto;
        }
    }
}
